import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { HttpResponse, HttpErrorResponse } from '@angular/common/http';
import { Observable } from 'rxjs';

import { IIncome } from 'app/shared/model/income.model';
import { IncomeService } from './income.service';

@Component({
    selector: 'jhi-income-update',
    templateUrl: './income-update.component.html'
})
export class IncomeUpdateComponent implements OnInit {
    private _income: IIncome;
    isSaving: boolean;

    constructor(private incomeService: IncomeService, private activatedRoute: ActivatedRoute) {}

    ngOnInit() {
        this.isSaving = false;
        this.activatedRoute.data.subscribe(({ income }) => {
            this.income = income;
        });
    }

    previousState() {
        window.history.back();
    }

    save() {
        this.isSaving = true;
        if (this.income.id !== undefined) {
            this.subscribeToSaveResponse(this.incomeService.update(this.income));
        } else {
            this.subscribeToSaveResponse(this.incomeService.create(this.income));
        }
    }

    private subscribeToSaveResponse(result: Observable<HttpResponse<IIncome>>) {
        result.subscribe((res: HttpResponse<IIncome>) => this.onSaveSuccess(), (res: HttpErrorResponse) => this.onSaveError());
    }

    private onSaveSuccess() {
        this.isSaving = false;
        this.previousState();
    }

    private onSaveError() {
        this.isSaving = false;
    }
    get income() {
        return this._income;
    }

    set income(income: IIncome) {
        this._income = income;
    }
}
